package com.ilink.groundservice.controller.AiX;


import com.ilink.groundservice.entity.VO.AiX.AiXSubPhaseResourceResponse;
import com.ilink.groundservice.service.AiX.AiXSceneSubPhaseService;
import com.ilink.ilinkapi.client.UserServiceClient;
import com.ilink.ilinkapi.dto.ground.AiX.AiXSubPhaseResourceInsertRequest;
import com.ilink.ilinkapi.dto.ground.AiX.AiXSubPhaseResourceUpdateRequest;
import com.ilink.ilinkapi.dto.ground.AiX.AiXSubPhaseUpdateRequest;
import com.ilink.ilinkcommon.domain.R;
import com.ilink.ilinkcommon.log.method.MethodLog;
import com.ilink.ilinkcommon.utils.UserContext;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RequestMapping("/ground/aix/sub_phase")
@RestController
@RequiredArgsConstructor
public class AiXSubPhaseController {
    
    private final AiXSceneSubPhaseService aiXSceneSubPhaseService;
    private final UserServiceClient userServiceClient;

    // 根据phaseId获取所有子阶段
    @MethodLog("根据phaseId获取所有子阶段")
    @GetMapping("/get_sub_phases")
    public R getSubPhases(@RequestParam("sceneId") Long sceneId,
    @RequestParam("phaseId") Long phaseId) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, sceneId);
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("view")) {
                log.error("用户没有查看该场景的权限，userId: " + userId + ", sceneId: " + sceneId);
                return R.error("您没有查看该场景的权限");
            }
            return aiXSceneSubPhaseService.getSubPhases(phaseId);
        } catch (Exception e) {
            log.error("根据phaseId获取所有子阶段失败，sceneId: " + sceneId + ", phaseId: " + phaseId, e);
            return R.error("根据phaseId获取所有子阶段失败");
        }
    }

    // 根据subPhaseId获取子阶段基本信息
    @MethodLog("根据subPhaseId获取子阶段基本信息")
    @GetMapping("/get_sub_phase_info")
    public R getSubPhaseInfo(@RequestParam("sceneId") Long sceneId,
    @RequestParam("subPhaseId") Long subPhaseId) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, sceneId);
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("view")) {
                log.error("用户没有查看该场景的权限，userId: " + userId + ", sceneId: " + sceneId);
                return R.error("您没有查看该场景的权限");
            }
            return aiXSceneSubPhaseService.getSubPhaseInfo(subPhaseId);
        } catch (Exception e) {
            log.error("根据subPhaseId获取子阶段基本信息失败，subPhaseId: " + subPhaseId, e);
            return R.error("根据subPhaseId获取子阶段基本信息失败");
        }
    }

    // 根据phaseId新增子阶段
    @MethodLog("根据phaseId新增子阶段")
    @PostMapping("/insert_sub_phase")
    public R insertSubPhase(@RequestParam("sceneId") Long sceneId,
    @RequestParam("phaseId") Long phaseId) { 
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, sceneId);
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("edit")) {
                log.error("用户没有编辑该场景的权限，userId: " + userId + ", sceneId: " + sceneId);
                return R.error("您没有编辑该场景的权限");
            }
            return aiXSceneSubPhaseService.insertSubPhase(sceneId, phaseId);
        } catch (Exception e) {
            log.error("根据phaseId新增子阶段失败，sceneId: " + sceneId + ", phaseId: " + phaseId, e);
            return R.error("根据phaseId新增子阶段失败");
        }
    }
    // 修改子阶段基本信息
    @MethodLog("修改子阶段基本信息") 
    @PostMapping("/update_sub_phase")
    public R updateSubPhase(@RequestBody AiXSubPhaseUpdateRequest request) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, request.getSceneId());
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("edit")) {
                log.error("用户没有编辑该场景的权限，userId: " + userId + ", sceneId: " + request.getSceneId());
                return R.error("您没有编辑该场景的权限");
            }
            return aiXSceneSubPhaseService.updateSubPhase(request);
        } catch (Exception e) {
            log.error("修改子阶段基本信息失败，subPhaseId: " + request.getSubPhaseId(), e);
            return R.error("修改子阶段基本信息失败");
        }
    }

    // 根据sceneId和subPhaseId删除子阶段
    @MethodLog("根据sceneId和subPhaseId删除子阶段")
    @PostMapping("/delete_sub_phase")
    public R deleteSubPhase(@RequestParam("sceneId") Long sceneId,
    @RequestParam("subPhaseId") Long subPhaseId) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, sceneId);
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("edit")) {
                log.error("用户没有编辑该场景的权限，userId: " + userId + ", sceneId: " + sceneId);
                return R.error("您没有编辑该场景的权限");
            }
            return aiXSceneSubPhaseService.deleteSubPhase(subPhaseId);
        } catch (Exception e) {
            log.error("根据sceneId和subPhaseId删除子阶段失败，sceneId: " + sceneId + ", subPhaseId: " + subPhaseId, e);
            return R.error("根据sceneId和subPhaseId删除子阶段失败");
        }
    }

    @MethodLog("查找子阶段所有资源")
    @GetMapping("/get_sub_phase_resources")
    public R getSubPhaseResources(@RequestParam("subPhaseId") Long subPhaseId,
        @RequestParam("sceneId") Long sceneId) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, sceneId);
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("view")) {
                log.error("用户没有查看该场景的权限，userId: " + userId + ", sceneId: " + sceneId);
                return R.error("您没有查看该场景的权限");
            }
            List<AiXSubPhaseResourceResponse> responseList = aiXSceneSubPhaseService.getSubPhaseResources(subPhaseId);
            if (responseList != null) {
                return R.ok("查找子阶段所有资源", responseList);
            } else {
                return R.error("查找子阶段所有资源失败", "未找到子阶段资源");
            }
        } catch (Exception e) {
            return R.error("查找子阶段所有资源失败", e.getMessage());
        }
    }

    @MethodLog("插入子阶段资源")
    @PostMapping("/insert_sub_phase_resource")
    public R insertSubPhaseResource(AiXSubPhaseResourceInsertRequest request) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, request.getSceneId());
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("edit")) {
                log.error("用户没有编辑该场景的权限，userId: " + userId + ", sceneId: " + request.getSceneId());
                return R.error("您没有编辑该场景的权限");
            }
            aiXSceneSubPhaseService.insertSubPhaseResource(request);
            return R.ok("插入子阶段资源");
        } catch (Exception e) {
            return R.error("插入子阶段资源失败", e.getMessage());
        }
    }

    @MethodLog("修改子阶段资源基本信息")
    @PostMapping("/update_sub_phase_resource")  
    public R updateSubPhaseResource(@RequestBody AiXSubPhaseResourceUpdateRequest request) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, request.getSceneId());
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("edit")) {
                log.error("用户没有编辑该场景的权限，userId: " + userId + ", sceneId: " + request.getSceneId());
                return R.error("您没有编辑该场景的权限");
            }
            aiXSceneSubPhaseService.updateSubPhaseResource(request);
            return R.ok("修改子阶段资源");
        } catch (Exception e) {
            return R.error("修改子阶段资源失败", e.getMessage());
        }
    }

    @MethodLog("修改子阶段资源封面图")
    @PostMapping("/update_sub_phase_resource_cover")
    public R updateSubPhaseResourceCover(@RequestParam("resourceId") Long resourceId,
        MultipartFile coverImage, @RequestParam("sceneId") Long sceneId) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, sceneId);
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("edit")) {
                log.error("用户没有编辑该场景的权限，userId: " + userId + ", sceneId: " + sceneId);
                return R.error("您没有编辑该场景的权限");
            }
            aiXSceneSubPhaseService.updateSubPhaseResourceCover(resourceId, coverImage);
            return R.ok("修改子阶段资源封面图");
        } catch (Exception e) {
            return R.error("修改子阶段资源封面图失败", e.getMessage());
        }
    }

    @MethodLog("删除子阶段资源")
    @PostMapping("/delete_sub_phase_resource")
    public R deleteSubPhaseResource(@RequestParam("resourceId") Long resourceId
    , @RequestParam("sceneId") Long sceneId) {
        try {
            String userId = UserContext.getUserId(); // 获取当前用户ID
            R permissions = userServiceClient.checkUserScenePermission(userId, sceneId);
            Map<String, Boolean> permissionsMap = (Map<String, Boolean>) permissions.getData();
            if (!permissionsMap.get("edit")) {
                log.error("用户没有编辑该场景的权限，userId: " + userId + ", sceneId: " + sceneId);
                return R.error("您没有编辑该场景的权限");
            }
            aiXSceneSubPhaseService.deleteSubPhaseResource(resourceId);
            return R.ok("删除子阶段资源");
        } catch (Exception e) {
            return R.error("删除子阶段资源失败", e.getMessage());
        }
    }
}
